<!--商品类型-->
<template>
  <div class="warehouse_box">
    <!--   查询模块-->
    <div class="search_box">
      <el-form
        ref="searchDate"
        :inline="true"
        :model="searchDate"
        class="demo-form-inline"
      >
        <el-form-item label="关键词：" prop="keyword">
          <el-input
            v-model="searchDate.keyword"
            placeholder="请输入关键词"
          ></el-input>
        </el-form-item>
        <el-form-item label="商品类型：" prop="type">
          <el-select v-model="searchDate.type">
            <el-option label="全部" value="全部"></el-option>
            <el-option label="桶装水" value="桶装水"></el-option>
            <el-option label="矿泉水" value="矿泉水"></el-option>
            <el-option label="空桶" value="空桶"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
          <el-button @click="resetForm()">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!--   新增模块-->
    <div class="add">
      <el-button type="primary" @click="openDialog(1)">新增</el-button>
    </div>

    <!--   数据列表-->
    <div class="commodity_list">
      <el-table
        :data="showDate"
        :header-cell-style="{
          background: 'rgba(227,189,189,0.53)',
          border: 'none',
          color: '#000',
        }"
        border
        style="width: 100%"
      >
        <el-table-column label="ID" width="130">
          <template slot-scope="scope">
            <p>{{ scope.row.id }}</p>
          </template>
        </el-table-column>
        <el-table-column label="修改时间" sortable width="180">
          <template slot-scope="scope">
            <p>{{ scope.row.updateTime }}</p>
          </template>
        </el-table-column>
        <el-table-column label="商品名称" prop="productName"> </el-table-column>
        <el-table-column label="logo">
          <template slot-scope="scope">
            <el-image
              :src="scope.row.icon"
              class="product_img"
              fit="fill"
              lazy
            ></el-image>
          </template>
        </el-table-column>
        <el-table-column label="商品类型" prop="type"> </el-table-column>
        <el-table-column label="销售价（元）" prop="salesPrice">
        </el-table-column>
        <el-table-column label="进货价（元）" prop="purchasePrice">
        </el-table-column>
        <el-table-column label="是否回收空桶">
          <template slot-scope="scope">
            <span :class="scope.row.drumsEmpty ? 'textGreen' : 'textRed'">
              {{ scope.row.drumsEmpty ? "是" : "否" }}</span
            >
          </template>
        </el-table-column>
        <el-table-column label="是否需要押金">
          <template slot-scope="scope">
            <span :class="scope.row.cashPledge ? 'textGreen' : 'textRed'">
              {{ scope.row.cashPledge ? "是" : "否" }}</span
            >
          </template>
        </el-table-column>
        <el-table-column label="状态" sortable>
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              active-color="#13ce66"
              inactive-color="#ff4949"
            >
            </el-switch>
            <span
              :class="scope.row.status ? 'textGreen' : 'textRed'"
              class="operation_btn"
              >{{ scope.row.status ? "启用" : "未启用" }}</span
            >
          </template>
        </el-table-column>
        <el-table-column label="操作" width="180">
          <template slot-scope="scope">
            <span class="operation_update" @click="openDialog(2, scope.row)"
              >编辑</span
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="list_bottom">
        <div class="list_length">
          共<span>{{ productList.length }}</span
          >条
        </div>
        <el-pagination
          :current-page="currentPage"
          :page-count="10"
          :page-size="pageSize"
          :page-sizes="[15, 20, 50, 100]"
          :total="productList.length"
          background
          layout=" prev, pager, next,sizes,jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        >
        </el-pagination>
      </div>

      <!--      编辑弹窗-->
      <Dialog
        ref="dialog"
        :dialogDate="updata"
        @addList="addList"
        @updateList="updateList"
      ></Dialog>
    </div>
  </div>
</template>
<script>
import Dialog from "./dialog";

export default {
  components: { Dialog },
  data() {
    return {
      searchDate: {
        keyword: "",
        type: "全部",
      },
      // 表格数据
      productList: [
        {
          id: 1,
          updateTime: "2021-04-16 16:35:38",
          productName: "娃哈哈矿物质水",
          icon: "https://qny.samt.top/2SN2472MG0wpji.jpg",
          type: "桶装水",
          salesPrice: 16,
          purchasePrice: 8,
          drumsEmpty: false,
          cashPledge: true,
          status: true,
        },
      ],
      updata: {},
      listSearch: [],
      currentPage: 1, // 当前页数
      pageSize: 15, // 每一页的数据个数
      showDate: [],
    };
  },
  mounted() {
    this.showTable();
  },
  methods: {
    // 数据切片分页渲染表格
    showTable() {
      // 加载表格数据,模拟请求到数据
      this.showDate = this.productList.slice(
        this.pageSize * (this.currentPage - 1),
        this.pageSize * this.currentPage
      );
    },
    //pageSize 改变时触发
    handleSizeChange(val) {
      // .slice((currentPage-1)*pageSize,currentPage*pageSize)
      this.pageSize = val;
      this.showTable();
    },
    // currentPage 改变时会触发的函数
    handleCurrentChange(val) {
      this.currentPage = val;
      this.showTable();
    },
    // 查询
    onSubmit() {
      this.$message({
        message:
          "关键字：" +
          this.searchDate.keyword +
          "商品类型:" +
          this.searchDate.type,
        type: "success",
      });
    },

    // 重置查询表单
    resetForm() {
      this.$refs.searchDate.resetFields();
    },

    // 拉起编辑弹窗，传入数据, type =（1 表示新增，2 表示修改）
    openDialog(type, list = "") {
      // 如果list为空不赋值
      if (list !== "") {
        // 解决弹窗数据改变，表格也随之改变，使用深拷贝 JSON.parse(JSON.stringify(list))
        this.updata = JSON.parse(JSON.stringify(list));
      }
      this.$refs.dialog.showDialog(type);
      // 调用子组件的开启
    },

    // 新增数据
    addList(list) {
      // 新增id
      let listDate = JSON.parse(list);
      // listDate.id = this.productList.length + 1;
      this.productList.push(listDate);
      this.showTable();
    },

    // 删除仓库数据
    deleteWarehouse(index) {
      this.productList.splice(index, 1);
      this.showTable();
    },
    // 接收弹窗返回的数据，修改原数据
    updateList(list) {
      // 遍历
      let listDate = JSON.parse(list);
      this.productList = this.productList.map((t) => {
        return t.id === listDate.id ? listDate : t;
      });
      this.showTable();
    },
  },
};
</script>
<style scoped>
.warehouse_box {
  display: flex;
  flex-direction: column;
}

.commodity_list {
  margin-top: 20px;
}

.operation_delete,
.operation_qurey,
.operation_update {
  cursor: pointer;
}

.operation_qurey,
.operation_update {
  color: #5749ff;
  margin: 0 5px;
}

.operation_delete {
  color: red;
  margin: 0 5px;
}

.list_length > span {
  color: #5749ff;
}

.commodity_list .list_bottom {
  display: flex;
  justify-content: space-between;
  margin-top: 20px;
  font-size: 1.5vh;
}

.textGreen {
  color: #42b983;
  font-weight: 700;
}

.textRed {
  color: #ff0000;
  font-weight: 700;
}

/deep/ .commodity_list .el-table .el-table__cell {
  padding: 0 !important;
}

.commodity_list .el-table_1_column_5 .cell {
}

.product_img {
  margin-top: 7px;
  width: 40px;
  height: 40px;
}
</style>
